﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wp.BLL;
using Wp.DAL;


public partial class Admin_Modules_Posts_PostEdit : System.Web.UI.Page
{
    public int? Id { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        Id = CommonHelper.QueryStringInt("Id", null);
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    void LoadData()
    {
        LoadDDLAuthor();

        if (!Id.HasValue)
        {
            CategoryList1.LoadData();
            return;
        }
        CategoryList1.LoadData(Id.Value);
        CategoryList1.LoadData();
        TblPostsEntity item = TblPostsManager.CreateInstant().SelectOne(Id.Value);
        txtTitle.Text = item.Title;
        txtSummary.Text = item.Summary;
        radEditorContent.Content = item.Description;
        ddlAuthor.SelectedValue = item.Author;
        ddlpublish.SelectedValue = item.PostStatus;
        imgThumb.ImageUrl = item.ImageUrl;
        hdfimg.Value = item.ImageUrl;
        txtTag.Text = TblTermRelationshipsManager.CreateInstant().GetStringTagsList(Id.Value, "posttags");

    }
    void LoadDDLAuthor()
    {
        List<UserEntity> ls = GetAllUser();
        ddlAuthor.DataSource = ls;
        ddlAuthor.DataTextField = "FullName";
        ddlAuthor.DataValueField = "UserName";
        ddlAuthor.DataBind();
    }
    public List<UserEntity> GetAllUser()
    {
        MembershipUserCollection uc = Membership.GetAllUsers();
        List<UserEntity> ls = new List<UserEntity>();
        foreach (MembershipUser user in uc)
        {
            UserEntity i = new UserEntity();
            i.UserName = user.UserName;
            ProfileCommon f = Profile.GetProfile(user.UserName);
            if (f != null)
                i.FullName = f.FullName;
            else i.FullName = user.UserName;
            ls.Add(i);
        }
        return ls;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int _Id = TblPostsManager.CreateInstant().InsertUpdate(GetTblPostsEntity());
        if (_Id != -1)
        {
            CategoryList1.Update(_Id);
            string[] str = txtTag.Text.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            TblTermRelationshipsManager.CreateInstant().DeleteByObjectIdAndTaxonomy(_Id, "posttags");
            for (int i = 0; i < str.Length; i++)
            {
                TblTermTaxonomyManager.CreateInstant().InsertPostTag(_Id, str[i], "posttags");
            }

        }
    }
    TblPostsEntity GetTblPostsEntity()
    {
        TblPostsEntity item = new TblPostsEntity();
        if (Id.HasValue)
        {
            item = TblPostsManager.CreateInstant().SelectOne(Id.Value);
        }
        else
        {
            item.TextId = TblPostsManager.CreateInstant().GetUniqueTextIdFromUnicodeText(txtTitle.Text.Trim());
            item.PostType = "post";
            item.Url = "";
            item.CreatedBy = HttpContext.Current.User.Identity.Name;
            item.CreatedDate = DateTime.Now;
            item.DisplayOrder = 1;
            item.ParentId = -1;
            item.CommentStatus = "allow";//prohibit
            item.PublishDate = new DateTime(1900, 1, 1);
        }
        item.Title = txtTitle.Text;
        item.Summary = txtSummary.Text.Trim();
        item.Description = radEditorContent.Content;
        item.ImageUrl = hdfimg.Value;
        item.Author = ddlAuthor.SelectedValue;
        item.ModifiedBy = HttpContext.Current.User.Identity.Name;
        item.ModifiedDate = DateTime.Now;
        item.PostStatus = ddlpublish.SelectedValue;
        return item;
    }


    public string Taxonomy { get; set; }
}